<template>
	<view>
		<view class="get-user">
			尊敬的{{customer_info.nickname}}（先生/女士）</br>
			为你产生有1张专属内部优惠券，请尽快使用！
		</view>
		<view class="all-coupon">
			<view class="coupon-details">
				<view class="coupon-details-left">
					<view class="">
						<text>￥</text>{{couponMsg.deduction_amount}}
					</view>
					<view class="">
						每个付费项目限制使用1张
					</view>
				</view>
				<view class="coupon-details-right">
					<view class="coupon-title">
						{{couponMsg.activity_title | goodName}}
					</view>
					<view class="flash-sale">
						<view class="">限时优惠</view>
						<view class="" v-if="couponMsg.customer_id == 0" @click="getCoupon">立刻领取</view>
						<view class="" v-else @click="useCoupon">去使用</view>
					</view>
					<view class="expire-time">
						到期时间：{{couponMsg.end_date}}
					</view>
				</view>
			</view>
		</view>
		<view class="coupon-explain">
			<view class="coupon-explain-title">优惠券说明：</view>
			<view class="common-coupon">1.每个优惠券都是独立的，分享一次只能领取一次。</view>
			<view class="common-coupon">2.请谨慎分享优惠券，如果家长使用优惠券，可以直接减免。可能会影响你的业绩提成。</view>
			<view class="common-coupon">3.每个项目每一个付费项目每1次只能使用1张优惠券。</view>
		</view>
	</view>
</template>

<script>
	export default{
		filters: {
			goodName(value) {
				if (!value) return ''
				if (value.length > 27) {
					return value.slice(0, 27) + '...'
				}
				return value
			},
		},
		data(){
			return{
				token:"",
				store_id:'',
				customer_info:"",
				activity_coupon_customer_id:'',
				activity_id:'',
				couponMsg:{}
			}
		},
		onLoad(options) {
			let pages = getCurrentPages()
			this.currentPge = pages[0].$route.fullPath
			this.activity_coupon_customer_id = options.activity_coupon_customer_id
			this.activity_id = options.activity_id
			let store_id = parseInt(options.store_id,10);
			console.log('进入了onload 获取url的store_id',store_id)
			if(!store_id > 0){
				store_id = uni.getStorageSync('store_id')?parseInt(uni.getStorageSync('store_id'),10):0
			}else{
				uni.setStorageSync('store_id', store_id)
			}
			let token = options.token;
			if(!token){
				token = uni.getStorageSync('token')
			}else{
				uni.setStorageSync('token', token);
			}
			this.token = token
			this.$store.commit('updateToken',token)
			this.store_id = parseInt(store_id,10)
			this.$store.commit('updateStoreId',store_id)
			console.log(this.store_id,'store_id')
			console.log(this.token,'token')
			console.log(this.$store.getters.getToken,'this.$store.getters.getToken')
			if(!this.$store.getters.getToken){
				// #ifdef H5
					location.href = this.$store.state.URL + "/customer/user/wx_login?page=" + this.currentPge
				// #endif
				// #ifdef MP-WEIXIN
					userLogin(this.share_id,this.invite_type,this.store_id,this.event_id).then((res)=>{
						console.log(res)
						console.log(555555)
					});
				// #endif
			}else{
				this.getCustomer()
			}
		},
		methods:{
			getCoupon(){
				let data = {
					token:this.$store.getters.getToken,
					activity_coupon_customer_id:this.couponMsg.activity_coupon_customer_id,
					customer_id:this.customer_info.customer_id
				}
				this.$api.get_coupon_customer(data).then(res => {
					console.log(res)
					if(res.status){
						uni.showToast({
							title:res.msg,
							icon:'none',
							duration:2000
						})
						setTimeout(()=>{
							this.gainCouponAct()
						},1000)
					}
				}).catch(err => {
					console.log(err)
				})
			},
			gainCouponAct(){
				let data = {
					token:this.$store.getters.getToken,
					activity_id:this.activity_id,
					activity_coupon_id:this.activity_coupon_id,
					activity_coupon_customer_id:this.activity_coupon_customer_id
					// page:1,
					// pageSize:this.pageSize
				}
				this.$api.get_activity_coupon_customer(data).then(res => {
					console.log(res)
					if(res.status){
						this.couponMsg = res.list.data[0]
						console.log(this.couponMsg,'活动优惠券列表')
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none',
							duration:2000
						})
					}
				}).catch(err => {
					console.log(err)
				})
			},
			getCustomer(){
				
				const data = {
					token:this.token,
					store_id:this.store_id
				}
				this.$api.getCustomerInfo(data).then(res => {
					console.log(res)
					if(res.status){
						this.customer_info = res.data.userinfo
						this.gainCouponAct()
						console.log(this.customer_info,'当前用户信息')
					}
				}).catch(err => {
					console.log(err)
				})
				// let customer_info = this.$api.getCustomerInfo(data)
				// console.log(this.customer_info)
				
			},
			useCoupon(){
				uni.redirectTo({
					url:'../../../../home/organization/activeDetail-3?page=coupon&coupon=' + JSON.stringify(this.couponMsg)
				})
			}
		}
	}
</script>

<style>
	page{
		background: #F3F3F4;
	}
	.get-user{
		width: 100%;
		height: 152upx;
		background: linear-gradient(to right,#FE9543,#FF664F);
		color: #FFFFFF;
		font-size: 30upx;
		text-align: center;
		padding-top: 40upx;
		box-sizing: border-box;
	}
	.all-coupon{
		width: 100%;
		padding: 30upx 20upx;
		box-sizing: border-box;
	}
	.coupon-details{
		width: 100%;
		height: 230upx;
		background: #FFFFFF;
		border-radius: 10upx;
		display: flex;
		margin-bottom: 20upx;
	}
	.coupon-details-left{
		width: 30%;
		height: 100%;
		background: url(https://tool.tuanhaoke.cn/static/image/member/active_background_coupon.png) no-repeat;
		background-size: 100% 100%;
		color: #FFFFFF;
		padding: 50upx 0;
		box-sizing: border-box;
		text-align: center;
	}
	.coupon-details-right{
		width: 70%;
		padding: 20upx;
		box-sizing: border-box;
	}
	.coupon-details-left>view:first-child{
		font-size:48upx;
	}
	.coupon-details-left>view>text{
		font-size: 32upx;
	}
	.coupon-details-left>view:last-child{
		font-size: 22upx;
		opacity: 0.8;
		padding: 0 30upx;
		box-sizing: border-box;
	}
	.coupon-title{
		height: 64upx;
		color: #333333;
		font-size: 28upx;
	}
	.flash-sale{
		width: 100%;
		display: flex;
		justify-content: space-between;
		margin-top: 26upx;
	}
	.flash-sale>view:first-child{
		color: #666666;
		font-size: 24upx;
		line-height: 60upx;
	}
	.flash-sale>view:last-child{
		width: 160upx;
		height: 60upx;
		line-height: 60upx;
		color: #FFFFFF;
		text-align: center;
		background: linear-gradient(to right,#FE9543,#FF664F);
		border-radius: 4upx;
		box-shadow: 0 6upx 10upx 0 rgba(166, 13, 0, 0.23);
		font-size: 24upx;
	}
	.expire-time{
		width: 100%;
		padding: 6upx 0;
		box-sizing: border-box;
		color: #999999;
		font-size: 24upx;
		margin-top: 6upx;
		border-top: 1upx dashed #D8D8D8;
	}
	.coupon-explain{
		width: 100%;
		padding: 30upx;
		box-sizing: border-box;
		background: #FFFFFF;
		
	}
	.coupon-explain-title{
		color: #333333;
		font-size: 30upx;
		font-weight: 900;
	}
	.common-coupon:first-child{
		margin-top: 30upx;
	}
	.common-coupon{
		color: #666666;
		font-size: 28upx;
		margin-top: 20upx;
	}
</style>
